//
//  GCMNoticeDetailViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/16.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMNoticeDetailViewController.h"


#import <WebKit/WebKit.h>


@interface GCMNoticeDetailViewController ()<WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>

@end

@implementation GCMNoticeDetailViewController{

    NSMutableDictionary *_dataDic;
    UIWebView *_webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self downLoadData];
}
-(void)downLoadData{
    
    [GCMMyNoticeHttpRequst postDataWithUserName:[GCMUserInfo instance].realName WithRealName:[GCMUserInfo instance].realName WithId:self.idd Success:^(id obj, NSError *err) {
        _dataDic = obj;
        //NSLog(@"html%@",_dataDic);
        [self createUI];
    }];
    
}
-(void)createUI{
    
    self.view.backgroundColor  = [UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1];
    
    UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(10, 69, SCREEN_WIDTH, 20)];
    NSString *str = [NSString stringWithFormat:@"%@-%@",[_dataDic valueForKey:@"title"],[GCMNoticeDetailViewController getDateFromString:[_dataDic valueForKey:@"pubTime"]]];
    label.text = str;
    label.textColor  = [UIColor lightGrayColor];
    [self.view addSubview:label];
    
    
    NSString *conStr = [[NSString alloc]init];
//    conStr = [[_dataDic valueForKey:@"content"]   stringByReplacingOccurrencesOfString:@"<p>" withString:@"  "];
//    conStr = [conStr  stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
//    conStr = [conStr  stringByReplacingOccurrencesOfString:@"\n" withString:@""];
//    conStr = [conStr  stringByReplacingOccurrencesOfString:@"\r" withString:@""];
//    conStr = [conStr  stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
////  根据文字定制label大小
//    CGSize size = CGSizeMake(SCREEN_WIDTH - 20,CGFLOAT_MAX);
//    UIFont* theFont = [UIFont systemFontOfSize:16];
//    //计算文字所占区域
//    CGSize labelSize = [conStr boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : theFont} context:nil].size;
//    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 94, SCREEN_WIDTH - 30 , labelSize.height+50)];
//    contentLabel.font = [UIFont systemFontOfSize:16];
//    contentLabel.textColor  = [UIColor colorWithRed:0.58 green:0.65 blue:0.65 alpha:1];
//    contentLabel.text = conStr;
//    [contentLabel setNumberOfLines:0];
//    contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
//    contentLabel.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:contentLabel];
    
    // 图片缩放的js代码
    NSString *js = @"var count = document.images.length;for (var i = 0; i < count; i++) {var image = document.images[i];image.style.width=150;};window.alert('找到' + count + '张图');";
    
    // 根据JS字符串初始化WKUserScript对象
    WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    // 根据生成的WKUserScript对象，初始化WKWebViewConfiguration
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [config.userContentController addUserScript:script];
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 94, SCREEN_WIDTH - 20, SCREEN_HEIGHT -  194)];
    //    _webView.UIDelegate = self;
    //    _webView.navigationDelegate = self;
    [_webView loadHTMLString:[_dataDic valueForKey:@"content"] baseURL:nil];
    [self.view addSubview:_webView];
    
    //UILabel *dateLabel  = [[UILabel alloc]initWithFrame:CGRectMake( 0 , 99 + contentLabel.bounds.size.height, SCREEN_WIDTH - 5, 20)];
     UILabel *dateLabel  = [[UILabel alloc]initWithFrame:CGRectMake( 0 , _webView.frame.origin.y + 10 +  _webView.bounds.size.height , SCREEN_WIDTH - 5, 20)];
    dateLabel.textColor  = [UIColor lightGrayColor];
    dateLabel.text = [_dataDic valueForKey:@"pubTime"];
    dateLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:dateLabel];
    
    

    
}

+ (NSString *)getDateFromString:(NSString *)dateStr
{
    NSArray *array = [dateStr componentsSeparatedByString:@" "];
    NSString *date = [array objectAtIndex:0];
    return date;
}

-(void)initData{

    _dataDic = [[NSMutableDictionary alloc]init];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
